﻿using System;
using System.Collections.Generic;
using System.Linq;
using Viettel.VOFFICE.DAO.Models;

namespace Viettel.VOFFICE.DAO.DAL.Repository
{
    /// <summary>
    /// xử lý tệp đính kèm
    /// </summary>
    public class AttachmentRepository : IRepository<Attachment>
    {
        private VOFFICEContext context;
        public AttachmentRepository(VOFFICEContext context)
        {
            if (context == null)
            {
                throw new Exception("Context must be init");
            }
            this.context = context;
        }

        public IEnumerable<Attachment> Get()
        {
            throw new NotImplementedException();
        }

        public Attachment GetById(object id)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<Attachment> FindByName(object s)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Đưa tệp đính kèm vào csdl
        /// </summary>
        /// <param name="T"></param>
        public void Insert(Attachment T)
        {
            if (null != T)
            {
                context.Attachments.Add(T);
                Save();
            }
        }

        public void Delete(object id)
        {
            throw new NotImplementedException();
        }

        public void Delete(Attachment T)
        {
            throw new NotImplementedException();
        }

        public void Update(Attachment T)
        {
            throw new NotImplementedException();
        }

        public void Save()
        {
            context.SaveChanges();
        }

        /// <summary>
        /// Lấy ra đính kèm từ id và loại truyền vào
        /// </summary>
        /// <param name="id"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public Attachment GetByIdAndType(int id, Enums.AttachmentType type)
        {
            return context.Attachments.FirstOrDefault(o => o.Id == id && o.ObjectType == (int)type);
        }
    }
}
